package com.util;

import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;

import static com.util.GaoDeMapUtils.*;


/**
 * @author Abell
 * @descibe 高德地图
 * @date 2020/10/27 10:05
 */
public class AmapUtil {

/*    private static String key = "56afc545d7f3e465118272d2cac81084";

    private static final Logger logger = LoggerFactory.getLogger(AmapUtil.class);
    */

    private static final Logger logger = LoggerFactory.getLogger(GaoDeMapUtils.class);

    /**
     * 高德api 根据地址获取经纬度
     *
     * @param name
     * @return
     *//*
    public static String getLatAndLogByName(String name) {
        StringBuffer s = new StringBuffer();
        s.append("key=" + key + "&address=" + name);
        String res = sendPost("http://restapi.amap.com/v3/geocode/geo", s.toString());
        logger.info(res);
        JSONObject jsonObject = JSONObject.fromObject(res);
        JSONArray jsonArray = JSONArray.fromObject(jsonObject.getString("geocodes"));
        JSONObject location = (JSONObject) jsonArray.get(0);
        String add = location.get("location").toString();
        return add;
    }

    *//**
     * 高德api 根据经纬度获取地址
     *
     * @param log
     * @param lat
     * @return
     *//*
    public static String getAddByAMAP(String log, String lat) {
        StringBuffer s = new StringBuffer();
        s.append("key=").append(key).append("&location=").append(log).append(",").append(lat);
        String res = sendPost("http://restapi.amap.com/v3/geocode/regeo", s.toString());
        logger.info(res);
        JSONObject jsonObject = JSONObject.fromObject(res);
        JSONObject jsonObject1 = JSONObject.fromObject(jsonObject.get("regeocode"));
        String add = jsonObject1.get("formatted_address").toString();
        return add;
    }

    *//**
     * 高德api 坐标转换---转换至高德经纬度
     *
     * @return
     *//*
    public static String convertLocations(String log, String lat, String type) {
        StringBuffer s = new StringBuffer();
        s.append("key=").append(key).append("&locations=").append(log).append(",").append(lat).append("&coordsys=");
        if (type == null) {
            s.append("gps");
        } else {
            s.append(type);
        }
        String res = sendPost("http://restapi.amap.com/v3/assistant/coordinate/convert", s.toString());
        logger.info(res);
        JSONObject jsonObject = JSONObject.fromObject(res);
        String add = jsonObject.get("locations").toString();
        return add;
    }*/

    public static void main(String[] args) {
        double log = 116.300108;
        double lat = 40.041313;
        String lonLat = getAddressByLonLat(log, lat);
        System.out.println(lonLat);
        HashMap<String, Object> map = new HashMap<>();
        JSONObject jo = JSONObject.fromObject(lonLat);

        String status = (String) jo.get("status");
        Object regeocode =  jo.get("regeocode");
        String info = (String) jo.get("info");
        String infocode = (String) jo.get("infocode");

        map.put("status",status);
        map.put("info",info);
        map.put("infocode",infocode);
        JSONObject jsonObject = JSONObject.fromObject(regeocode);
        Object addressComponent =  jsonObject.get("addressComponent");
        String formatted_address = (String) jsonObject.get("formatted_address");
        map.put("formatted_address",formatted_address);
        JSONObject address = JSONObject.fromObject(addressComponent);
        Object city =  address.get("city");
        String province = (String) address.get("province");
        String adcode = (String) address.get("adcode");
        String district = (String) address.get("district");
        String towncode = (String) address.get("towncode");
        map.put("city",city);
        map.put("province",province);
        map.put("adcode",adcode);
        map.put("district",district);
        map.put("towncode",towncode);
        System.out.println(map);



    }
}

